#!/bin/bash
#增加Oracle Grid节点，一次可以增加多个节点

HAS_ERROR=0
function DO_CMD() {
    echo Exec command: $@
    $@
    EXIT_CODE=$?
    if [ $EXIT_CODE != 0 ]; then
        HAS_ERROR=1
        echo ERROR: Execute $@ failed.
        exit $EXIT_CODE
    else
        echo FINE: Execute success.
    fi
}

function SUDO_ORA_CMD() {
    USER=$1
    shift
    echo "Exec command: su - '$USER' -c '$@'"
    eval "su - '$USER' -c '$@'"
    exitCode=$?
    if [ $exitCode -ge 128 ]; then
        #负数返回码，大于127就是负数的补码
        echo "ERROR: Execute command failed, exitCode:$exitCode."
        exit $exitCode
    elif [ $exitCode -gt 0 ]; then
        echo "WARN: Execute command  has some warning, exitCode:$exitCode."
    else
        echo "FINE: All operations were successful."
    fi
}

function SUDO_CMD_CONT() {
    USER=$1
    shift
    echo "Exec command: su - '$USER' -c '$@'"
    eval "su - '$USER' -c 'AUTOEXEC_JOBID=$AUTOEXEC_JOBID $@'"
    EXIT_CODE=$?
    if [ $EXIT_CODE != 0 ]; then
        HAS_WARN=1
        echo WARN: Execute $@ failed.
    else
        echo FINE: Execute success.
    fi
}

#参数处理,全部用长参数
######################################
function parseOpts() {
    OPT_SPEC=":h-:"
    while getopts "$OPT_SPEC" optchar; do
        case "${optchar}" in
        -)
            case "${OPTARG}" in
            CV_ASSUME_DISTID)
                CV_ASSUME_DISTID="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            GRID_USER)
                GRID_USER="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            NODE_NAMES)
                NODE_NAMES="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            NODE_VIP_NAMES)
                NODE_VIP_NAMES="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            NODE_TYPE)
                NODE_TYPE="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            *)
                if [ "$OPTERR" = 1 ] && [ "${OPT_SPEC:0:1}" != ":" ]; then
                    echo "Unknown option --${OPTARG}" >&2
                fi
                ;;
            esac
            ;;
        h)
            usage
            exit 2
            ;;
        *)
            if [ "$OPTERR" != 1 ] || [ "${OPT_SPEC:0:1}" = ":" ]; then
                echo "Non-option argument: '-${OPTARG}'" >&2
            fi
            ;;
        esac
    done
}

parseOpts "$@"

if [ -z "$GRID_USER" ]; then
    GRID_USER=grid
fi

ORACLE_UID=$(id -u $GRID_USER 2>/dev/null)
if [ -z "$ORACLE_UID" ]; then
    echo "WARN: User $GRID_USER not exists, maybe already uninstalled."
fi

ENV_DISTID=""
if [ "$CV_ASSUME_DISTID" != "AUTO" ]; then
    ENV_DISTID="CV_ASSUME_DISTID=$CV_ASSUME_DISTID"
fi

oraEnv=$(SUDO_ORA_CMD $GRID_USER env | grep ORACLE_)
echo "$oraEnv"
eval "$oraEnv"

#========开始执行删除操作==============================
if [ -z "$INVENTORY_LOCATION" ]; then
    INVENTORY_LOCATION=$(cat $ORACLE_HOME/oraInst.loc | grep inventory_loc | sed s'/inventory_loc\s*=\s*//' | sed s'/\s*$//')
fi

if [ -z "$INVENTORY_LOCATION" ]; then
    echo "ERROR: Can not determin invertory directory by file:$GRID_HOME/oraInst.loc."
    exit 3
fi

echo "Try to verify add node preparation..."
SUDO_ORA_CMD $GRID_USER $ENV_DISTID $ORACLE_HOME/bin/cluvfy stage -pre nodeadd -n $NODE_NAMES -verbose -fixup

ADD_NODE_CMD=$ORACLE_HOME/addnode/addnode.sh
ADD_NODE_CMD=$ORACLE_HOME/addnode/addnode.sh
if [ ! -e "$ADD_NODE_CMD" ]; then
    if [ -e "$ORACLE_HOME/oui/bin/addNode.sh" ]; then
        #Oracle 11 12
        ADD_NODE_CMD=$ORACLE_HOME/oui/bin/addNode.sh
    fi
fi

echo "Try to execute addnode command to add node $NODE_NAMES for cluster..."
if [ "$NODE_TYPE" != "none" ]; then
    SUDO_ORA_CMD $GRID_USER $ENV_DISTID IGNORE_PREADDNODE_CHECKS=Y $ADD_NODE_CMD \
        -silent \
        -ignorePrereq -ignoreSysPrereqs \
        "CLUSTER_NEW_NODES={$NODE_NAMES}" \
        "CLUSTER_NEW_VIRTUAL_HOSTNAMES={$NODE_VIP_NAMES}" \
        "CLUSTER_NEW_NODE_ROLES={$NODE_TYPE}"
else
    SUDO_ORA_CMD $GRID_USER $ENV_DISTID IGNORE_PREADDNODE_CHECKS=Y $ADD_NODE_CMD \
        -silent \
        -ignorePrereq -ignoreSysPrereqs \
        "CLUSTER_NEW_NODES={$NODE_NAMES}" \
        "CLUSTER_NEW_VIRTUAL_HOSTNAMES={$NODE_VIP_NAMES}"
fi

exit $HAS_ERROR
